﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace HttpUploader5.demoAccess.db
{
	/// <summary>
	/// 注意，输出的文件路径会进行UrlEncode编码
	/// 客户端需要进行UrlDecode解码
	/// </summary>
	public partial class ajax_list : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string uid = Request.QueryString["uid"];
			StringBuilder sb = new StringBuilder();
			sb.Append("[");

			if (!string.IsNullOrEmpty(uid))
			{
				HttpUploaderDB db = new HttpUploaderDB();
				DataTable tb = new DataTable();
				db.GetFilesByUid(Convert.ToInt32(uid), ref tb);

				List<string> ls = new List<string>();
				foreach(DataRow row in tb.Rows)
				{
					StringBuilder item = new StringBuilder();
					item.Append("{s:0");
					foreach (DataColumn col in tb.Columns)
					{
						item.Append(",");
						item.Append(col.ColumnName);
						item.Append(":");
						item.Append("\"");

						if (col.ColumnName == "FilePathLocal"
							|| col.ColumnName == "FilePathRemote"
							|| col.ColumnName == "FilePathRelative")
						{
							string v = HttpUtility.UrlEncode(row[col.ColumnName].ToString());
							//UrlEncode会将空格解析成+号，
							v = v.Replace("+","%20");
							item.Append(v);
						}
						else
						{
							item.Append(row[col.ColumnName]);
						}
						item.Append("\"");
					}
					item.Append("}");
					ls.Add(item.ToString());
				}
				string[] strs = ls.ToArray();
				sb.Append(string.Join(",", strs));
			}
			sb.Append("]");
			Response.Write(sb.ToString());
		}
	}
}
